package domain.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import util.SpringUtils;
import domain.dao.interfaces.IDAOUser;
import domain.entities.interfaces.IUser;

public class DAOUser extends HibernateDaoSupport implements IDAOUser {

	private HibernateTemplate hibernateTemplate;


	
	public void init() {
		setHibernateTemplate(getSessionFactory());
	}

	public void setHibernateTemplate(SessionFactory sessionFactory){
		this.hibernateTemplate= new HibernateTemplate(sessionFactory);
	}

	/* (non-Javadoc)
	 * @see domain.dao.impl.IDAOUser#addUser(org.hibernate.Session, java.lang.String, java.lang.String)
	 */
	public int addUser(
			final String login, final String password) throws Exception {

		ApplicationContext context = SpringUtils.getApplicationContext();


		IUser user = (IUser) context.getBean("beanUser");
		user.setLogin(login);
		user.setPassword(password);


		hibernateTemplate.save(user);


		return user.getId();
	}

	/* (non-Javadoc)
	 * @see domain.dao.impl.IDAOUser#getAll()
	 */
	public List<IUser> getAll() {

		return (List<IUser>) hibernateTemplate
				.execute(new HibernateCallback<List<IUser>>() {

					public List<IUser> doInHibernate(Session session)
							throws HibernateException, SQLException {


						//Business code
						Query query = session.createQuery("from User");
						return query.list();
					}
				});
	}

	/* (non-Javadoc)
	 * @see domain.dao.impl.IDAOUser#getByLogin(java.lang.String)
	 */
	public IUser getByLogin(final String login) {

		return (IUser) hibernateTemplate
				.execute(new HibernateCallback<IUser>() {

					public IUser doInHibernate(Session session)
							throws HibernateException, SQLException {


						//Business code
						Query query = session.createQuery("from User where login = :login");
						query.setParameter("login", login);
						List result = query.list();
						
						return result.size() > 0 ? 
								(IUser) query.list().get(0) : 
									null;
					}
				});
	}

	/* (non-Javadoc)
	 * @see domain.dao.impl.IDAOUser#getById(int)
	 */
	public IUser getById(final int id) {
		return (IUser) hibernateTemplate
				.execute(new HibernateCallback<IUser>() {

					public IUser doInHibernate(Session session)
							throws HibernateException, SQLException {


						//Business code
						Query query = session.createQuery("from User where id = :id");
						query.setParameter("id", id);
						List result = query.list();
						
						return result.size() > 0 ? 
								(IUser) query.list().get(0) : 
									null;
					}
				});
	}
}
